<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 引入头部组件 -->
      <PageTools>
        <span slot="after">
          <el-button type="primary" plain size="small" @click="addRooT(1, '0')"
            >添加权限</el-button
          >
        </span>
      </PageTools>
      <!--  -->
      <el-card>
        <el-table
          style="width: 100%; margin-bottom: 20px"
          row-key="id"
          border
          :data="list"
          default-expand-all
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="name" label="菜单名称" sortable>
          </el-table-column>
          <el-table-column prop="code" label="权限标识" sortable>
          </el-table-column>
          <el-table-column prop="description" label="描述"> </el-table-column>
          <el-table-column label="操作" width="340px">
            <!-- 获取row数据 -->
            <el-row slot-scope="{ row }">
              <!-- 只有一级标签有添加权限 -->
              <el-link
                type="primary"
                style="margin: 0 8px 0 0"
                :underline="false"
                v-if="row.type == 1 ? true : false"
                @click="addRooT(2, row.id)"
                >添加权限</el-link
              >
              <el-link
                type="success"
                style="margin: 0 8px 0 0"
                :underline="false"
                >查看api权限</el-link
              >

              <el-link
                style="margin: 0 8px 0 0"
                :underline="false"
                @click="bianjiRoot(row.id)"
                >编辑权限</el-link
              >

              <el-link type="info" style="margin: 0 8px 0 0" :underline="false"
                >查看</el-link
              >
              <el-link
                type="danger"
                :underline="false"
                @click="deleteRoot(row.type, row.id)"
                >删除权限</el-link
              >
            </el-row>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 添加权限的弹窗部分 -->
    <el-dialog :title="formDate.id?'编辑权限':'新增权限'" :visible="addrootTC" width="30%" @close="btnNO">
      <el-row>
        <el-form label-width="80px" :model="formDate" :rules="rules" ref="formdata">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formDate.name" />
          </el-form-item>
          <!-- 

     -->
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formDate.code" />
          </el-form-item>
          <!-- 

     -->
          <el-form-item label="权限描述">
            <el-input v-model="formDate.description" />
          </el-form-item>
          <!-- 

     -->
          <el-form-item label="企业可见">
            <el-switch
              v-model="formDate.enVisible"
              active-text="可见"
              inactive-text="不可见"
              active-value="1"
              inactive-value="2"
            />
          </el-form-item>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="btnNO">取 消</el-button>
        <el-button type="primary" size="small" @click="btnOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//获取权限的详情api
import {
  getPermission,
  addRoot,
  deleteRoot,
  getPermissionParticulars,
  updateParticulars,
} from "@/api/permission";
// 树状图的组件
import { tranListToTreeData } from "@/utils";
export default {
  data() {
    return {
      list: [],
      // 弹窗的开关
      addrootTC: false,
      // 弹出层绑定
      formDate: {
        // 菜单名称
        name: "",
        // 权限标识
        code: "",
        // 权限描述
        description: "",
        // PID
        pid: "",
        // type
        type: "",
        // 开关
        enVisible: "",
      },
      // 表单验证
      rules: {
        // 验证权限名
        name: [{ required: true, message: "权限名不能为空", trigger: "blur" }],
        // 验证权限标识
        code: [
          { required: true, message: "权限标识不能为空", trigger: "blur" },
        ],
      },
    };
  },
  // 初始化获取程序
  created() {
    this.getPermission();
  },
  methods: {
    // 这里是获取数据并且调用递归函数转换成树状图
    async getPermission() {
      const RES = await getPermission();
      this.list = tranListToTreeData(RES, "0");
      console.log(RES);
    },
    // 点击添加权限
    addRooT(type, id) {
      this.formDate.type = type;
      this.formDate.pid = id;
      this.addrootTC = true;
    },
    // 点击弹窗的确定按钮
     btnOk() {
         this.$refs.formdata.validate().then(async ()=>{
             if (this.formDate.id) {
     
      return   await updateParticulars(this.formDate);
      }
   // 添加权限

   return   await addRoot(this.formDate);

        }).then(()=>{
           this.$message.success("操作成功");
      // 关闭弹窗
      this.addrootTC = false;
      // 重新拉取数据
      this.getPermission();
        })
   
   
    },
    // 点击弹窗的取消按钮
    btnNO() {
      // 清空弹窗的内容
      this.formDate = {};
      // 关闭弹窗
      this.addrootTC = false;
    },
    // 删除权限得控制范围
    async deleteRoot(id) {
      try {
        await deleteRoot(id);
        //删除成功
        this.$message.success("删除成功");
        //  重新拉取数据
        this.getPermission();
      } catch (EER) {
        console.log(EER);
      }
    },
    // 获取相应的数据编辑频道
    async bianjiRoot(id) {
        this.formDate = await getPermissionParticulars(id);
        this.addrootTC = true;
    },
  },
};
</script>

<style>
</style>